﻿package 
{
	import flash.events.Event;
	import flash.display.Sprite;

	public class LineChart extends Chart
	{
		protected var stageWidth:Number;
		protected var stageHeight:Number;
		protected var largestPoint:Number;

		public function LineChart()
		{
			super();
		}

		public override function ParseData(inputData:Object)
		{
			super.ParseData(inputData);

			largestPoint = 0;
			chartAttribute = inputData['name'];
			attributeNames = new Array();
			if ('Total' in inputData)
			{
				dataPoints[0] = new Array();
				for (var date:String in inputData['Total'])
				{
					var dataPoint:Number = parseFloat(inputData['Total'][date].replace(/,/g,''));
					if (dataPoint > largestPoint)
					{
						largestPoint = dataPoint;
					}
					dataPoints[0].push(dataPoint);
					attributeNames.push(date);
				}
			} else if ('Subattr' in inputData) {
				var dpIndex:int = 0;
				for (var subattrs:String in inputData['Subattr'])
				{
					if (! 'Total' in inputData['Subattr'][subattrs])
					{
						break;
					}

					dataPoints[dpIndex] = new Array();
					attributeNames.push(subattrs);
					for (var date1:String in inputData['Subattr'][subattrs]['Total'])
					{
						var dataPoint1:Number = parseFloat(inputData['Subattr'][subattrs]['Total'][date1].replace(/,/g,''));
						if (dataPoint1 > largestPoint)
						{
							largestPoint = dataPoint1;
						}
						dataPoints[dpIndex].push(dataPoint1);
					}
					dpIndex++;
				}
			}
			
			if(dataPoints.length > 1)
			{
				try {
					dataPoints.sort(SortDataPoints);
				} catch (e:Error) {
					DebugCallback(e.name + ': ' + e.message);
				}
			}
			Init(stageWidth, stageHeight);
		}

		public function ReValidateData()
		{
			largestPoint = 0;
			for (var i:int = 0; i < dataPoints.length; i++)
			{
				for (var j:int = 0; j < dataPoints[i].length; j++)
				{
					if (dataPoints[i][j] > largestPoint)
					{
						largestPoint = dataPoints[i][j];
					}
				}
			}
			if(dataPoints.length > 1)
			{
				dataPoints.sort(SortDataPoints);
			}
		}

		protected override function ScaleY(dataPoint:Number):Number
		{
			return stageHeight - (dataPoint / largestPoint * (stageHeight / 2));
		}

		public function Init(sw:Number, sh:Number)
		{
			stageWidth = sw;
			stageHeight = sh;
			if (dataPoints.length <= 0)
			{
				return void;
			}
		
			graphics.clear();
			for (var i:int = 0; i < dataPoints.length; i++)
			{
				var spread:Number = stageWidth / dataPoints[i].length;
				var currentSpread:Number = 0;
				graphics.lineStyle(2, colorArray[i]);
				graphics.beginFill(colorArray[i], 0.5);
				var yScale:Number = ScaleY(dataPoints[i][0]);
				graphics.moveTo(currentSpread, yScale);
	
				for (var j:int = 0; j < dataPoints[i].length; j++)
				{
					yScale = ScaleY(dataPoints[i][j]);
					currentSpread += spread;
					graphics.lineTo(currentSpread, yScale);
				}
	
				graphics.lineStyle(1, 0x666666);
	
				graphics.lineTo(stageWidth, stageHeight + 1);
				graphics.lineTo(-1, stageHeight + 1);
	
				yScale=ScaleY(dataPoints[i][0]);
				graphics.lineTo(-1, yScale);
				graphics.endFill();
			}

			super.DrawControls();
		}
	}
}